<?php
require('includes/application_top.php');
require(DIR_WS_LANGUAGES . $language . '/pricematch.php');

function to_money($string)
{
$string = preg_replace("|\,|","",$string);
$Full = split("[\.]",$string);
$Count = count($Full);
if($Count > 1)
{
$First = $Full[0];
$Second = $Full[1];
if($Second == "0000")
{
  $Second = "00";
}
$NumCents = strlen($Second);

if($NumCents == 2)
{
}
else if($NumCents < 2)
{
$Second = $Second . "0";
}
else if($NumCents > 2)
{
$Temp = substr($Second,0,3);
$Rounded = round($Temp,-1);
$Second = substr($Rounded,0,2);
}
}
else
{
$First = $Full[0];
$Second = "00";
}
$length = strlen($First);
if( $length <= 3 )
{
$string = $First . "." . $Second;
return $string;
}
else
{
$loop_count = intval( ( $length / 3 ) );
$section_length = -3;
for( $i = 0; $i < $loop_count; $i++ )
{
$sections[$i] = substr( $First, $section_length, 3 );
$section_length = $section_length - 3;
}
$stub = ( $length % 3 );
if( $stub != 0 )
{
$sections[$i] = substr( $First, 0, $stub );
}
$Done = implode( ",", array_reverse( $sections ) );
$Done = $Done . "." . $Second;
return $Done;
}
}
?>

<html>
<head>

<script language="javascript"  type="text/javascript">

<!-- Sends the form data to the pricematch_complete.php after validating it a bit //-->
function sndPricematch(product,price,c_price,c_website,name,email,code) {
    if(validateForm(pricematch) == true) {
    url = 'index2.php?option=com_oscommerce&osMod=pricematch_complete&product='+product+'&price='+price+'&c_price='+c_price+'&c_website='+c_website+'&name='+name+'&email='+email+'&code='+code;
    self.location = url;
  }
  else
  {
  }
}

<!-- Simple validation check of form data //-->
function validateForm(pricematch) {
var emailFilter=/^.+@.+\..{2,4}$/;
if(""==document.forms.pricematch.Competitor_Price.value)
{
alert("<?php echo PRICEMATCH_PRICE_ERROR; ?>");
document.forms.pricematch.Competitor_Price.focus();
return false;
}
else if(""==document.forms.pricematch.Competitor_Website.value)
{
alert("<?php echo PRICEMATCH_URL_ERROR; ?>");
document.forms.pricematch.Competitor_Website.focus();
return false;
}
else if(""==document.forms.pricematch.Name.value)
{
alert("<?php echo PRICEMATCH_NAME_ERROR; ?>");
document.forms.pricematch.Name.focus();
return false;
}
else if(""==document.forms.pricematch.email.value)
{
alert("<?php echo PRICEMATCH_EMAIL_ERROR1; ?>");
document.forms.pricematch.email.focus();
return false;
}
else if (!(emailFilter.test(document.forms.pricematch.email.value))) {
alert("<?php echo PRICEMATCH_EMAIL_ERROR1; ?>");
document.forms.pricematch.email.focus();
return false;
}
else
return true;
}


</script>

<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body>
<table class="main" width="100%" height="100%" bgcolor="#ffffff"><tr><td valign="top" align="center">
<?php
$this_Code = $_REQUEST['code1'];
$this_Product = $_REQUEST['product1'];
$this_Price = $_REQUEST['price1'];
?>
<div class="contentheading"><?php echo PRICEMATCH_HEADING; ?></div><br>
<form name="pricematch">
<table border="0" width="400" cellspacing="5" cellpadding="5" style="border-collapse: collapse" class="infoBoxContents">
	<tr>
		<td><?php echo PRICEMATCH_PRODUCT_NAME; ?></td>
		<td>

		<p><input type="hidden" name="Product_Name" size="40" value="<?php echo $this_Product; ?>">
		<?php echo $this_Product; ?>
		</td>
	</tr>
	<tr>
		<td><?php echo PRICEMATCH_OUR_PRICE; ?></td>
		<td><input type="hidden" name="Price" size="10" value="<?php echo to_money($this_Price); ?>">
        <?php echo to_money($this_Price); ?></td>
	</tr>
	<tr>
		<td><?php echo PRICEMATCH_COMPETITOR_PRICE; ?></td>
		<td><input type="text" name="Competitor_Price" size="10"></td>
	</tr>
	<tr>
		<td><?php echo PRICEMATCH_COMPETITOR_WEBSITE; ?></td>
		<td><input type="text" name="Competitor_Website" size="30"></td>
	</tr>
	<tr>
		<td><?php echo PRICEMATCH_CUSTOMER_NAME; ?></td>
		<td><input type="text" name="Name" size="30"></td>
	</tr>
	<tr>
		<td><?php echo PRICEMATCH_CUSTOMER_EMAIL; ?></td>
		<td><input type="text" name="email" size="30">
        <input type="hidden" name="code" value="<?php echo $this_Code;?>"</td>
	</tr>
</table><br>
<input type="Button" value="Submit" name="B1" onclick="sndPricematch(Product_Name.value,Price.value,Competitor_Price.value,Competitor_Website.value,Name.value,email.value,code.value)"></p>
		</form>
</td></tr></table>
</body></html>
